﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cn.Edwon.Web.Support;
using System.Reflection;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Spring;
using Cn.Cellula.Model.Security;
using Cn.Edwon.Web;
using Cn.Cellula.Client;
using Cn.Edwon.Web.Validation;
using Cn.Cellula.Biz;

namespace Cn.Cellula.Website.Admin.User {
    public partial class EditOfficeInfo : PageSupport {
        IUserBiz biz = BizFactory.Get<IUserBiz>();
        String userKey = null;
        protected void Page_Load(object sender, EventArgs e) {
            userKey = (Request.QueryString["id"] == null) ? this.Actor.ID : Request.QueryString["id"];
            if (!Page.IsPostBack) {
                InitForm();
                if (this.Actor != null) {
                    FillForm();
                }
            }
        }
        private void FillForm() {
            try {
                VSysUserEO user = biz.Load<VSysUserEO>(userKey);
                this.lab_LoginName.Text = user.LoginName;
                this.lab_UserName.Text = user.UserName;
                this.txt_Dept.Text = user.DeptName;
                this.hid_Dept.Value = user.DeptKey;
                this.txt_Duty.Text = user.Duty;
                this.btn_save.Enabled = PowerBiz.UserEdit(this.Actor, user).Editable;
            } catch (Exception ex) {
                log.Error(ex.Message);
                SiteInfo.Send(Page, TxtDef.ERROR, "装载用户数据时错误");
            }
        }
        private void InitForm() {
        }

        protected void btn_save_Click(object sender, EventArgs e) {
            Boolean bk = QuickValidator.Execute(Request, this.hid_Dept.Parent);
            if (!bk) {
                SiteInfo.Send(Page, TxtDef.ERROR, QuickValidator.Message);
                return;
            }
            try {
                TSysUserEO user = biz.Load<TSysUserEO>(userKey);
                user.DeptKey = this.hid_Dept.Value;
                user.Duty = this.txt_Duty.Text;
                biz.Update<TSysUserEO>(user);
                BizFactory.Get<ILogBiz>().LogUpdate("办公资料", user.LoginName, this.Actor, Request);
                SiteInfo.SendSucceed(Page, "");
            } catch (Exception ex) {
                log.Error("更新用户资料时出现异常:" + ex.Message);
                SiteInfo.Send(Page, TxtDef.FAILED, "更新办公信息时出现错误");
            }
        }

    }
}